package cn.edu.njust.protobuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ProtobufServerHandler extends SimpleChannelInboundHandler<Message.WrappedMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message.WrappedMessage msg) throws Exception {
        switch (msg.getMessageType()){
            case REQUEST:
                log.info("收到客户端请求：message={}", msg.getRequest().getMessage());
                ctx.writeAndFlush(
                        Message.WrappedMessage
                        .newBuilder()
                        .setMessageType(Message.WrappedMessage.MessageType.RESPONSE)
                        .setResponse(
                                Message.Response
                                        .newBuilder()
                                        .setStatus(200)
                                        .setMessage("服务端响应")
                                        .build()
                        )
                        .build()
                );
                break;
        }
    }
}
